// pages/evaluate/evaluate.js
import {
  Get,
  Post,
  JsonPost,
  API_URL
} from "../../utils/request.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subInfo: {
      order_id: '',
      data:[
        {
          goods_id:null,
          content:'',
          img:[],
        },
        {
          goods_id:null,
          content:'',
          img:[],
        },
      ]
    },
    info: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let arr=[]
    JSON.parse(options.row).goods.forEach(ele => {
      arr.push({
        goods_id:ele.id,
        content:'',
        img:[],
      })
    });
    let str='subInfo.order_id'
    let str1='subInfo.data'
    this.setData({
      [str]: JSON.parse(options.row).id,
      [str1]: arr,
      info: JSON.parse(options.row)
    })
  },


  uploadImg(e) {
    let that = this
    if (this.data.subInfo.data[e.currentTarget.dataset.index].img.length > 2) {
      return
    }
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: API_URL + 'user/image',
          filePath: tempFilePaths[0],
          name: 'upload',
          header: {
            'X-Access-Token': wx.getStorageSync('token')
          },
          formData: {
            'upload': tempFilePaths[0]
          },
          success(re) {
            let img = JSON.parse(re.data).data
            let str = "subInfo.data["+e.currentTarget.dataset.index+"].img"
            let arr = JSON.parse(JSON.stringify(that.data.subInfo.data[e.currentTarget.dataset.index].img))
            arr.push(img)
            that.setData({
              [str]: arr,
            })
          }
        })
      }
    })
  },

  bindTextAreaBlur(e) {
    let str = "subInfo.data["+e.currentTarget.dataset.index+"].content"
    this.setData({
      [str]: e.detail.value
    })
  },

  subm() {
    let Data = this.data.subInfo
    let ifReturn=false
    Data.data.some(ele => {
      if (ele.content.trim() == '') {
        wx.showToast({
          title: '请添加评论',
          icon: 'none',
          duration: 2000
        })
        ifReturn=true
        return true
      }
    });
    if (ifReturn) {
      return
    }
    JsonPost('evaluate/add', Data).then(res => {
      wx.showToast({
        title: '评论成功',
        duration: 2000
      })
      wx.navigateBack()
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})